//
//  ViewController.m
//  05_ScrollView练习
//
//  Created by dllo on 16/7/20.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ViewController.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UIScrollViewDelegate>
@property (nullable, nonatomic, strong)UIPageControl *page;
@property (nullable, nonatomic, strong)UIScrollView *scrollView;
@property (nullable, nonatomic, strong)UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    _scrollView.contentSize = CGSizeMake(WIDTH * 4, HEIGHT);
   // _scrollView.contentOffset = CGPointMake(WIDTH * 5, 0);
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.bounces = NO;
    [self.view addSubview:_scrollView];
    NSArray *arr = [NSArray arrayWithObjects:@"guidePage_01", @"guidePage_02", @"guidePage_03", @"guidePage_04", nil];
    for (int i = 0; i < arr.count; i++) {
        UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(i * WIDTH, 0, WIDTH, HEIGHT)];
        scroll.bounces = NO;
        scroll.minimumZoomScale = 1.0;
        scroll.maximumZoomScale = 2.0;
        scroll.delegate = self;
        [_scrollView addSubview:scroll];
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        _imageView.image = [UIImage imageNamed:arr[i]];
        _imageView.userInteractionEnabled = YES;
//        for (UIScrollView *scr in _scrollView.subviews) {
//            if ([scr isKindOfClass:[UIScrollView class]]) {
//                scr.zoomScale = 1;
//            }
//        }
        [scroll addSubview:_imageView];
    }
    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(50, HEIGHT - 50, WIDTH - 100, 40)];
    _page.numberOfPages = arr.count;
    _page.currentPage = 0;
    [self.view addSubview:_page];
    [_page addTarget:self action:@selector(clickPage:) forControlEvents:UIControlEventValueChanged];
    
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}
- (void)clickPage:(UIPageControl *)page {
    self.scrollView.contentOffset = CGPointMake(WIDTH * page.currentPage, 0);
    
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    _page.currentPage = _scrollView.contentOffset.x / WIDTH;

}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    for (UIScrollView *scr in scrollView.subviews) {
        if ([scr isKindOfClass:[UIScrollView class]]) {
            scr.zoomScale = 1;
        }
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
